<template>
  <div class="login">
    <div class="logo">
      <img src="../assets/logo.jpg" alt="MyLogo" />
    </div>
    <!-- 手机号 -->
    <InputGroup
      type="number"
      v-model="phone"
      placeholder="手机号"
      :btnTitle="btnTitle"
      :disabled="disabled"
      :error="errors.phone"
      @btnclick="getVerifyCode"
    ></InputGroup>
    <!-- 验证码 -->
    <InputGroup
      type="number"
      v-model="verifyCode"
      placeholder="验证码"
      :error="errors.code"
    ></InputGroup>
    <div class="login_des">
      <p>
        新用户登录即可自动注册，表示已同意
        <span>《用户服务协议》</span>
      </p>
    </div>
    <div class="login_btn">
      <button :disabled="isClick" @click="login">登录</button>
    </div>
  </div>
</template>
<script>
import InputGroup from "../components/InputGroup";
export default {
  name: "login",
  data() {
    return {
      phone: "",
      verifyCode: "",
      errors: {},
      btnTitle: "获取验证码",
      disabled: false,
      id: "",
    };
  },
  computed: {
    isClick() {
      if (!this.phone || !this.verifyCode) return true;
      else return false;
    },
  },
  methods: {
    login() {
      //取消错误提醒
      this.errors = {};
      this.id = new Date().getTime();
      //发送请求
      this.$axios
        .post("/api", {
          mobile: this.phone,
          id: this.id,
        })
        .then((res) => {
          let userInfo = res.config.data;
          //检验成功，设置登录状态并跳转到/
          localStorage.setItem("ele_login", userInfo);
          this.$router.push("/");
        })
        .catch((err) => {
          //返回错误信息
          this.errors = {
            code: err.response.data.msg,
          };
        });
    },
    getVerifyCode() {
      if (this.validatePhone()) {
        //发送网络请求
        this.validateBtn();
        const code = 1234;
        const text =
          "验证码：" +
          code +
          ",您正在使用登陆功能,该验证码仅用于身份验证,在五分钟之内有效，请勿泄露给其他人使用。";
        let param = new URLSearchParams();
        param.append("Uid", "fch324");
        param.append("Key", "d41d8cd98f00b204e980");
        param.append("smsMob", this.phone);
        param.append("smsText", text);
        this.$axios
          .post("/sms_api", param, {
            //post请求，在请求时会自动把param拼接再Param后面
            headers: {
              Accept: "application/json, text/plain, */*",
              "Content-Type": "application/json; charset=UTF-8",
            },
          })
          .then((res) => {
            console.log(res);
          });
      }
    },
    validatePhone() {
      if (!this.phone) {
        this.errors = {
          phone: "手机号码不能为空",
        };
        return false;
      } else if (!/^1[3456789]\d{9}/.test(this.phone)) {
        this.errors = {
          phone: "请填写正确的号码",
        };
        return false;
      } else {
        this.errors = {};
        return true;
      }
    },
    validateBtn() {
      let time = 60;
      let timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer);
          this.btnTitle = "获取验证码";
          this.disabled = false;
        } else {
          this.btnTitle = `${time--}秒后重试`;
          this.disabled = true;
        }
      }, 1000);
    },
  },
  components: { InputGroup },
};
</script>
<style scoped>
.login {
  width: 100%;
  height: 100%;
  padding: 30px;
  box-sizing: border-box;
  background: #fff;
}
.logo {
  text-align: center;
}
.logo img {
  width: 200px;
}
.text_group,
.login_des,
.login_btn {
  margin-top: 10px;
}
.login_des {
  color: #aaa;
  line-height: 22px;
}
.login_des p {
  font-size: 12px;
}
.login_des span {
  color: #4d90fe;
}
.login_btn button {
  width: 100%;
  height: 40px;
  background-color: #48ca38;
  border-radius: 4px;
  color: white;
  font-size: 14px;
  border: none;
  outline: none;
}
.login_btn button[disabled] {
  background-color: #8bda81;
}
</style>